//
//  HMUserAccountViewModel.swift
//  Weibo-swift
//
//  Created by GD on 16/9/22.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit
//封装 授权页面的网络请求  -----对应的视图 是 HMOAuthViewController
class HMUserAccountViewModel: NSObject {
    //单例  全局访问点   ----用户模型对象 全局共享
    static let sharedUserAccountViewModel: HMUserAccountViewModel = HMUserAccountViewModel()
    
    //用户归档用户对象
    var userAccount:HMUserAccount?{//viewModel 对本地数据的处理.
        //从沙盒路径里面获取
        return HMUserAccount.loadUserAccount()
    }
    
    var isLogin:Bool {
        return accessToken != nil
    }
    
    
    //判读AccessToken是否过期
    var accessToken: String? {
        let token = userAccount?.access_token
        
        //获取过期时间对象
        let expiresDate = userAccount?.expiresDate
        
        //如果Token存在,判断其是否过期.
        if let t = token {
            //表示accessToken不为nil
            //let a = expiresDate?.compare(Date())
            if expiresDate?.compare(Date()) == .orderedDescending {//表示不过期
                return t
            }else{//过期
                return nil
            }
        }else{
            return nil
        }
    }
    
    
//    var set:String {
//        if userAccount?.uid == 0 {
//            return "nan"
//        }else{
//            return "nv"
//        }
//    }
    
    
    
    //获取Token
    func requestAccessToken(code:String,callBack:@escaping (Bool)->()) {//此处闭包中可传多个参数
        HMNetworkTools.sharedTools.requestAccessToken(code: code) { (response, error) in
            if error != nil{
                print("网络有问题:\(error)")
                callBack(false)
                return
            }
            
            // 转模型print(response)
            guard let dict = response as? [String: Any] else {
                print("字典有问题")
                callBack(false)
                return
            }
            
            let userAccount = HMUserAccount(dict: dict)
            //获取用户信息
            print(userAccount.access_token)
            
            self.requestUserInfo(userAccount: userAccount,callBack: callBack)
        }
    }
    
    private func requestUserInfo(userAccount:HMUserAccount,callBack:@escaping (Bool)->()) -> Void {
        HMNetworkTools.sharedTools.requestUserInfo(accessToken: userAccount.access_token!, uid: userAccount.uid, callBack: { (response, error) in
            if error != nil{
                callBack(false)
                print("网络有问题:\(error)")
                return
            }
            
            // 转模型print(response)
            guard let dict = response as? [String: Any] else {
                print("字典有问题")
                callBack(false)
                return
            }
            
            let name = dict["name"]
            let profile_image_url = dict["profile_image_url"]
            
            userAccount.name = name as? String
            userAccount.profile_image_url = profile_image_url as! String?
            
            print(userAccount.name)
            
            userAccount.saveUserAccount()
            callBack(true)//登录成功

        })
    }
}
